package com.ruoyi.web.controller.cloudroom;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysRoomUser;
import com.ruoyi.system.domain.vo.cloudroom.bean.vo.Room;
import com.ruoyi.system.service.ISysRoomUserService;
import com.ruoyi.system.service.cloudroom.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/room")
public class RoomController extends BaseController {
    @Autowired
    private RoomService roomService;

    @Autowired
    private ISysRoomUserService sysRoomUserService;

    /**
     * 邀请
     * @param room
     * @return
     */
    @PostMapping(value = "/invite")
    public AjaxResult invite(@RequestBody Room room) {
        String result;
        try {
            String targetUrl = "gb28181:44010200402000000123";
            if (room.getTargetUrl() != null) {
                targetUrl = room.getTargetUrl();
            }
            result = roomService.invite(room.getRoomId(), room.getUid(), room.getNickName(), targetUrl);
        } catch (Exception e) {
            log.warn("invite:" + e.getMessage());
            return error("邀请:" + room.getNickName() + "失败");
        }
        return AjaxResult.parseResult(result);
    }

    /**
     * 踢人
     * @param room
     * @return
     */
    @PostMapping(value = "/kickOut")
    public AjaxResult kickOut(@RequestBody Room room) {
        String result;
        try {
            result = roomService.kickOut(room.getRoomId(),room.getUid());
        } catch (Exception e) {
            log.warn("kickOut:" + e.getMessage());
            return error("踢人:" + room.getUid() + "失败");
        }
        return AjaxResult.parseResult(result);
    }

    @GetMapping(value = "/queryGbList")
    public AjaxResult queryGbList(String deviceNum) {
        String result;
        try {
            result = roomService.queryGbList(deviceNum);
        } catch (Exception e) {
            log.warn("queryGbList:" + e.getMessage());
            return error("queryGbList失败");
        }
        return AjaxResult.parseResult(result);
    }

    /**
     * 新增房间创建信息
     */
    @PostMapping(value = "/addSysRoomUser")
    public AjaxResult addSysRoomUser(@RequestBody SysRoomUser sysRoomUser)
    {
        return toAjax(sysRoomUserService.insertSysRoomUser(sysRoomUser));
    }

    @GetMapping(value = "/getInfo/{roomId}")
    public AjaxResult getInfo(@PathVariable("roomId") Long roomId)
    {
        return success(sysRoomUserService.selectSysRoomUserByRoomId(roomId));
    }
}
